<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>src\settings.js - GIAnT API</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.9.1/build/cssgrids/cssgrids-min.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <link rel="stylesheet" href="../assets/css/main.css" id="site_styles">
    <link rel="icon" href="../assets/favicon.ico">
    <script src="http://yui.yahooapis.com/combo?3.9.1/build/yui/yui-min.js"></script>
</head>
<body class="yui3-skin-sam">

<div id="doc">
    <div id="hd" class="yui3-g header">
        <div class="yui3-u-3-4">
                <h1><img src="../assets/css/logo.png" title="GIAnT API" width="117" height="52"></h1>
        </div>
        <div class="yui3-u-1-4 version">
            <em>API Docs for: 1.0.0</em>
        </div>
    </div>
    <div id="bd" class="yui3-g">

        <div class="yui3-u-1-4">
            <div id="docs-sidebar" class="sidebar apidocs">
                <div id="api-list">
                    <h2 class="off-left">APIs</h2>
                    <div id="api-tabview" class="tabview">
                        <ul class="tabs">
                            <li><a href="#api-classes">Classes</a></li>
                            <li><a href="#api-modules">Modules</a></li>
                        </ul>
                
                        <div id="api-tabview-filter">
                            <input type="search" id="api-filter" placeholder="Type to filter APIs">
                        </div>
                
                        <div id="api-tabview-panel">
                            <ul id="api-classes" class="apis classes">
                                <li><a href="../classes/Atom.html">Atom</a></li>
                                <li><a href="../classes/Codec.html">Codec</a></li>
                                <li><a href="../classes/Constraints.html">Constraints</a></li>
                                <li><a href="../classes/Database.html">Database</a></li>
                                <li><a href="../classes/Exif.html">Exif</a></li>
                                <li><a href="../classes/Export.html">Export</a></li>
                                <li><a href="../classes/Heatmap.html">Heatmap</a></li>
                                <li><a href="../classes/Server.html">Server</a></li>
                                <li><a href="../classes/Settings.html">Settings</a></li>
                                <li><a href="../classes/Utils.html">Utils</a></li>
                            </ul>
                
                
                            <ul id="api-modules" class="apis modules">
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="yui3-u-3-4">
                <div id="api-options">
                    Show:
                    <label for="api-show-inherited">
                        <input type="checkbox" id="api-show-inherited" checked>
                        Inherited
                    </label>
            
                    <label for="api-show-protected">
                        <input type="checkbox" id="api-show-protected">
                        Protected
                    </label>
            
                    <label for="api-show-private">
                        <input type="checkbox" id="api-show-private">
                        Private
                    </label>
                    <label for="api-show-deprecated">
                        <input type="checkbox" id="api-show-deprecated">
                        Deprecated
                    </label>
            
                </div>
            
            <div class="apidocs">
                <div id="docs-main">
                    <div class="content">
<h1 class="file-heading">File: src\settings.js</h1>

<div class="file">
    <pre class="code prettyprint linenums">
var fs = require(&#x27;fs&#x27;);
var path = require(&#x27;path&#x27;);
var log = require(&#x27;electron-log&#x27;);

/**
 *
 * Settings
 * --------
 * Gives easy access to a key value storage persisted in a single file. &lt;br&gt;
 *
 * &lt;b&gt;Takes care of the validation of editor settings and constraint settings.&lt;/b&gt;
 *
 * @class Settings
*/
var Settings = Settings || {};

/**
 * In memory store
 *
 * @property _store
 * @type {object}
 * @default {}
 */
Settings._store = {};

/**
 * Whether the in-memory-store was already populated
 *
 * @property _loaded
 * @type {boolean}
 * @default false
 */
Settings._loaded = false;

/**
 * The path of the settings file
 *
 * @property _file_path
 * @type {string}
 * @default path.join(__dirname, &#x27;../media/settings/settings.json&#x27;)
 */
Settings._file_path = path.join(__dirname, &#x27;../media/settings/settings.json&#x27;);

/**
* Loads the file
 *
* @method load
*/
Settings.load = function() {
    var json;
    try {
        json = JSON.parse(fs.readFileSync(Settings._file_path, &#x27;utf8&#x27;));
    } catch (e) {
        log.error(&#x27;Could not load settings&#x27;);
        log.error(e);
        json = {};
    }
    Settings._store = json;
    Settings._loaded = true;
};

/**
* Saves the file
 *
* @method save
*/
Settings.save = function() {
    // in order to avoid a flush we assure the settings were loaded first
    Settings.check_loaded();
    fs.writeFileSync(Settings._file_path, JSON.stringify(Settings._store, null, 4));
};

/**
* If the storage is not loaded -&gt; load it.
 *
* @method check_loaded
*/
Settings.check_loaded = function() {
    if (!Settings._loaded) {
        Settings.load();
    }
};

/**
* Set the key to the value
 *
* @method set
* @param key {string}
* @param value {*}
*/
Settings.set = function(key, value) {
    Settings.check_loaded();
    Settings._store[key] = value;
};

/**
* Get the value of the key
 *
* @method get
* @param key {string}
* @param fallback {*}
* @return {*}
*/
Settings.get = function(key, fallback) {
    Settings.check_loaded();
    if (!Settings.has(key)) {
        return fallback;
    }
    return Settings._store[key];
};

/**
* Check whether the key carries data in the storage
 *
* @method has
* @param key {string}
* @return {boolean}
*/
Settings.has = function(key) {
    Settings.check_loaded();
    return Settings._store.hasOwnProperty(key);
};

/**
* Returns the whole storage
 *
* @method get_settings_for_frontend
* @return {*}
*/
Settings.get_settings_for_frontend = function() {
    Settings.check_loaded();
    return Settings._store;
};

/**
* Check the values of the new settings and store them if they are okay
 *
* @method set_settings_from_frontend
* @param new_settings {*}
*/
Settings.set_settings_from_frontend = function(new_settings) {
    try {
        var fontSize = new_settings.fontSize;
        if (8 &lt; fontSize &amp;&amp; fontSize &lt; 200) {
            Settings._store.styles.defaultVertex.fontSize = fontSize;
        }
        var strokeWidth = new_settings.strokeWidth;
        if (0 &lt; strokeWidth &amp;&amp; strokeWidth &lt; 200) {
            Settings._store.defaultEdgeStyle.strokeWidth = strokeWidth;
        }
        var curved = new_settings.curved;
        if (curved === &quot;1&quot;) {
            Settings._store.defaultEdgeStyle.curved = &quot;1&quot;;
        } else {
            Settings._store.defaultEdgeStyle.curved = &quot;0&quot;;
        }
        Settings.save();
    } catch (e) {
        log.error(&#x27;settings could not be applied&#x27;);
    }
};

/**
* Get the constraint settings Settings._store.constraints
 *
* @method get_settings_for_constraints
* @return {*}
*/
Settings.get_settings_for_constraints = function() {
    Settings.check_loaded();
    return Settings._store.constraints;
};

module.exports = Settings;
    </pre>
</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>
<script src="../assets/js/yui-prettify.js"></script>
<script src="../assets/../api.js"></script>
<script src="../assets/js/api-filter.js"></script>
<script src="../assets/js/api-list.js"></script>
<script src="../assets/js/api-search.js"></script>
<script src="../assets/js/apidocs.js"></script>
</body>
</html>
